<?php

declare(strict_types=1);

namespace app\common\service\sys;

use app\common\traits\ServiceTrait;
use app\common\dao\sys\UserGroupDao;
use app\common\service\hrm\DeptService;
use app\common\util\TreeUtil;

/**
 * 系统用户分组 服务业务层(Service)
 * @author 恺哥
 * @date  2022-07-13
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class UserGroupService
{
  protected $dao = null;

  public function __construct()
  {
    $this->dao = new UserGroupDao();
  }
  use ServiceTrait;
  /**
   * 重写 搜索条件
   * @param $post
   * @return mixed
   */
  public function _list_where($post)
  {
    $where = [];
    $order = ['id' => 'desc'];
    $field = ['*'];
    if (isset($post['pid'])) {
      if ($post['pid'] == 0) {
        $where[] = ['pid', '=', 0];
      } else {
        $where[] = ['pid', '>', 0];
        $where[] = ['pid', '=', $post['pid']];
      }
    } else {
      $where[] = ['id', '=', 0];
    }

    $result['where'] = $where;
    $result['field'] = $field;
    $result['order'] = $order;
    return $result;
  }

  /**
   * 获取分组-用户树形 -选择用户时用
   */
  public function getUserGroupLayuiTree()
  {
    $list =  $this->dao->getListAll(true, ['sort' => 'asc'], ['id,pid,name as title,sort']);
    // 取组对应关联用户ID表
    if ($list) {
      foreach ($list as $k => $v) {
        $ullist = (new UserGroupMemberService())->getUserByGroup($v['id']);
        $ul = $ullist['data'];
        if ($ul) {
          foreach ($ul as $k1 => $v1) {
            if (isset($v1['avatar'])) {
              $ul[$k1]['avatar'] = _img($v1['avatar']);
            }
          }
          $list[$k]['list'] = $ul;
        }
      }
    }
    //树型
    $list = TreeUtil::list_to_tree($list, 0, 'id', 'pid', 'children');
    return $list;
  }

  /**
   * 解析根据分组、部门选择的结果
   * @param string $data
   * @return array 用户ID 集 例(1,3,4)
   */
  public function parseUgdc($data)
  {
    if (isset($data) && !empty($data)) {
      // if (strpos($data, ',') !== false) {
      $us = new UserService();
      $ds = new DeptService();
      $ugs = new UserGroupMemberService();
      $arr = explode(',', $data);
      $ids = [];
      foreach ($arr as $v) {
        if (strpos($v, 'c') !== false) { //部门根---部门必须绑定公司ID
          //no1:获取根对应的公司ID
          $mid = str_replace('c', '', $v);
          $tmp = $ds->getInfoById($mid);
          $company_id = $tmp['company_id'];
          //no2根据公司ID取所有会员id
          $tmpids = $us->getUserIdByCompanyId($company_id);
          $ids = array_merge_recursive($ids, $tmpids);
        } elseif (strpos($v, 'd') !== false) { //部门id
          $dept_id = str_replace('d', '', $v);
          $tmpids = $us->getUserIdByDeptId($dept_id);
          $ids = array_merge_recursive($ids, $tmpids);
        } elseif (strpos($v, 'g') !== false) { //组id
          $group_id = str_replace('g', '', $v);
          $tmpids = $ugs->getUserIdByGroupId($group_id);
          $ids = array_merge_recursive($ids, $tmpids);
        } elseif (strpos($v, 'u') !== false) { //会员ID
          $user_id = str_replace('u', '', $v);
          $tmpids = [];
          $tmpids[] = $user_id;
          $ids = array_merge_recursive($ids, $tmpids);
        }
      }
      //IDS 去重。并去key取值
      $n_ids = array_values(array_unique($ids)); // 如[1,3,8]
      return $n_ids;
      // }
    }
    return [];
  }
}
